
mediasources_mpas <- readRDS("./domain_political_proxmity_def.RDS")

# calculate insularity_plus_difference = insularity max + GINI coeff

mediasources_mpas$max <- apply(mediasources_mpas[,2:11],1, max) # max

library(DescTools)
mediasources_mpas$gini <- apply(mediasources_mpas[,2:11],1,Gini)
mediasources_mpas$insularity <- (mediasources_mpas$max+mediasources_mpas$gini)/2

im <- mean(mediasources_mpas$insularity)
isd <- sd(mediasources_mpas$insularity)

# Insularity based adjudication

# Insularity
mediasources_mpas$insularity_class[mediasources_mpas$insularity > im+isd] <- "High" 
mediasources_mpas$insularity_class[mediasources_mpas$insularity > im & mediasources_mpas$insularity <= im+isd] <- "Moderate"
mediasources_mpas$insularity_class[mediasources_mpas$insularity < im & mediasources_mpas$insularity >= im-isd] <- "Low"
mediasources_mpas$insularity_class[mediasources_mpas$insularity < im-isd] <- "None"

# MP-MPAS
mediasources_mpas$adjudication <- colnames(mediasources_mpas[2:11])[max.col(mediasources_mpas[2:11],ties.method="random")]
mediasources_mpas$adjudication[mediasources_mpas$insularity_class == "None"] <- "Cross-partisan"


saveRDS(mediasources_mpas, file = "./mediasources_mpas_insularity.rds")